package com.iespring.service.admin.configmanager;

import com.alibaba.druid.sql.visitor.functions.If;
import com.iespring.bean.admin.configmanager.bo.ConfigOrderBo;
import com.iespring.bean.admin.configmanager.bo.ConfigWxBo;
import com.iespring.bean.admin.configmanager.vo.ConfigWxVo;
import com.iespring.bean.common.BasePageInfo;
import com.iespring.bean.common.BaseRespVo;
import com.iespring.mapper.admin.ConfigSystemMapper;
import com.iespring.util.configmanager.ConfigKeyValue;
import com.iespring.util.configmanager.ConfigManagerUtils;
import org.apache.ibatis.transaction.Transaction;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * \* Auth: 杨宇健
 * \* Time: 2022/12/31 13:59
 * \* Description:
 * \
 */

@Service
public class ConfigManagerServiceImpl implements ConfigManagerService {
    @Resource
    ConfigSystemMapper configSystemMapper;

    @Transactional
    @Override
    public BaseRespVo update(Object configBo) {

        List<ConfigKeyValue> keyValueList = ConfigManagerUtils.getKeyValueList(configBo);

        int i = configSystemMapper.updateByKeyValueList(keyValueList);

        //TODO 定义异常，事务回滚

        if (i == keyValueList.size()) {
            return BaseRespVo.ok();
        } else return BaseRespVo.ok();

    }

    @Override
    public BaseRespVo selectOrder() {

        List<ConfigKeyValue> list = configSystemMapper.selectOrder();
        ConfigOrderBo configOrderBo = new ConfigOrderBo();
        if (list.size() == 0) {
            return BaseRespVo.error("未查询到结果", 500);
        }

        //-- 101: '未付款', 102: '用户取消', 103: '系统取消',
        // 201: '已付款', 202: '申请退款', 203: '已退款',
        // 301: '已发货', 401: '用户收货', 402: '系统收货'

        Map<String, String> map = ConfigManagerUtils.listToMap(list);
        //确认收货后超期 天 取消评论资格 401, 402 ->
        configOrderBo.setMarket_order_comment(map.get("market_order_comment"));
        //订单发货后超期 天 系统自动收货  301 -> 402
        configOrderBo.setMarket_order_unconfirm(map.get("market_order_unconfirm"));
        //用户下单后超时 分 系统自动取消  101 -> 103
        configOrderBo.setMarket_order_unpaid(map.get("market_order_unpaid"));
        return BaseRespVo.ok(configOrderBo);


    }

    @Override
    public BaseRespVo wx() {
        List<ConfigKeyValue> list = configSystemMapper.selectWx();
        ConfigWxVo configWxVo = new ConfigWxVo();
        if (list.size() == 0) {
            return BaseRespVo.error("未查询到结果", 500);
        }
        Map<String, String> map = ConfigManagerUtils.listToMap(list);
        configWxVo.setMarket_wx_index_new(map.get("market_wx_index_new"));
        configWxVo.setMarket_wx_index_topic(map.get("market_wx_index_topic"));
        boolean share = "true".equals(map.get("market_wx_share")) ? true : false;
        configWxVo.setMarket_wx_share(share);
        configWxVo.setMarket_wx_index_brand(map.get("market_wx_index_brand"));
        configWxVo.setMarket_wx_catlog_goods(map.get("market_wx_catlog_goods"));
        configWxVo.setMarket_wx_catlog_list(map.get("market_wx_catlog_list"));
        configWxVo.setMarket_wx_index_hot(map.get("market_wx_index_hot"));
        return BaseRespVo.ok(configWxVo);
    }


}
